//
//  ShopCartCell.m
//  FD
//
//  Created by Yasin on 2018/10/25.
//  Copyright © 2018 ZhiYuan Network. All rights reserved.
//

#import "ShopCartCell.h"

@interface ShopCartCell()
/**背景*/
@property (nonatomic,  strong) UIView *bgView;

/**1.标题*/
@property (nonatomic,  strong) UILabel *nameLb;
/**2.折扣价 红色*/
@property (nonatomic,  strong) UILabel *discountPriceLb;
/**3.原价 灰色下划线*/
@property (nonatomic,  strong) UILabel *originPriceLb;
/**4.右边加减按钮 先红色占位*/
@property (nonatomic,  strong) UIView *addMinulView;
/**5.减号按钮*/
@property (nonatomic,  strong) UIButton *minusBtn;
/**6.数量*/
@property (nonatomic,  strong) UILabel *countLb;
/**7.加号按钮*/
@property (nonatomic,  strong) UIButton *addBtn;

@end

@implementation ShopCartCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupView];
    }
    return self;
}
- (void)setupView {
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.nameLb];
    [self.bgView addSubview:self.discountPriceLb];
    [self.bgView addSubview:self.originPriceLb];
    
    //右边的加减按钮
    [self.bgView addSubview:self.addMinulView];
    [self.addMinulView addSubview:self.minusBtn];
    [self.addMinulView addSubview:self.countLb];
    [self.addMinulView addSubview:self.addBtn];
}

- (void)layoutSubviews {
    //必须要有
    [super layoutSubviews];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    [self layoutLeftView];
    [self layoutRightView];
}

//布局左边的view
- (void)layoutLeftView {
    //cellH : 50
    //名字:青椒肉故事盖饭
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(10);
        make.top.equalTo(self.bgView.mas_top).offset(5);
        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];
    [self.discountPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLb.mas_left);
        make.top.equalTo(self.nameLb.mas_bottom).offset(2);
        make.size.mas_equalTo(CGSizeMake(60, 20));
    }];
    [self.originPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.discountPriceLb.mas_right);
        make.top.equalTo(self.discountPriceLb.mas_top);
        make.size.mas_equalTo(CGSizeMake(60, 20));
    }];
    
   
}
//布局右边的view
- (void)layoutRightView {
    //加减按钮
    [self.addMinulView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView.mas_right).offset(0);
        make.centerY.equalTo(self.bgView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(70, 30));
    }];
    [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.addMinulView.mas_left);
        make.centerY.equalTo(self.addMinulView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [self.countLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.minusBtn.mas_right);
        make.centerY.equalTo(self.addMinulView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.countLb.mas_right);
        make.centerY.equalTo(self.addMinulView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
}

#pragma mark -setter
- (void)setModel:(ShopCartModel *)model {
    _model = model;
    self.nameLb.text =model.title;
    self.discountPriceLb.text = [NSString stringWithFormat:@"￥%.1f",[model.discount_price floatValue]];
    self.originPriceLb.text = [NSString stringWithFormat:@"￥%.1f",[model.original_price floatValue]];
    self.countLb.text = [NSString stringWithFormat:@"%@",model.quantity];
}

#pragma mark -getter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}
- (UILabel *)nameLb {
    if (!_nameLb) {
        _nameLb = [UILabel new];
        _nameLb.text = @"青椒肉丝盖浇饭";
    }
    return _nameLb;
}
- (UILabel *)discountPriceLb {
    if (!_discountPriceLb) {
        _discountPriceLb = [UILabel new];
        _discountPriceLb.text = @"￥12.0";
        _discountPriceLb.font = [UIFont systemFontOfSize:16.0*kScaleFit];
//        _discountPriceLb.backgroundColor = [UIColor redColor];
    }
    return _discountPriceLb;
}

- (UILabel *)originPriceLb {
    if (!_originPriceLb) {
        _originPriceLb = [UILabel new];
        _originPriceLb.text = @"￥20.0";
        _originPriceLb.textColor = [UIColor lightGrayColor];
        _originPriceLb.font = [UIFont systemFontOfSize:15.0*kScaleFit];
//        _originPriceLb.backgroundColor = [UIColor brownColor];
    }
    return _originPriceLb;
}
- (UIView *)addMinulView {
    if (!_addMinulView) {
        _addMinulView = [UIView new];
//        _addMinulView.backgroundColor = [UIColor redColor];
    }
    return _addMinulView;
}
- (UIButton *)minusBtn {
    if (!_minusBtn) {
        _minusBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_minusBtn setImage:[UIImage imageNamed:@"jian"] forState:(UIControlStateNormal)];
        [_minusBtn addTarget:self action:@selector(clickMinusBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _minusBtn;
}
- (UILabel *)countLb {
    if (!_countLb) {
        _countLb = [UILabel new];
        _countLb.text = @"1";
        _countLb.textAlignment = NSTextAlignmentCenter;
        _countLb.font = [UIFont systemFontOfSize:14.0*kScaleFit];
    }
    return _countLb;
}

- (UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_addBtn setImage:[UIImage imageNamed:@"jia"] forState:(UIControlStateNormal)];
        [_addBtn addTarget:self action:@selector(clickAddBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _addBtn;
}

#pragma mark -点击事件
//制作事件回调，不处理逻辑问题
- (void)clickMinusBtnAction:(UIButton *)btn {
    if (self.shopcartCellMinusBlock) {
        self.shopcartCellMinusBlock();
    }
}
- (void)clickAddBtnAction:(UIButton *)btn {
    if (self.shopcartCellAddBlock) {
        self.shopcartCellAddBlock();
    }
}


@end
